from PySide6.QtWidgets import (
    QWidget,
    QHBoxLayout,
    QVBoxLayout,
    QLabel,
    QComboBox,
    QLineEdit,
    QPushButton,
    QMessageBox,
)


class LatLonToFloat(QWidget):
    """
    Latitude and longitude convert floating-point numbers
    """

    def __init__(self, parent=None):
        super().__init__(parent)

        self.setWindowTitle("经纬度转换浮点数")
        self.setObjectName("经纬度转换浮点数")

        self.label_lon = QLabel("经度：")
        self.lon_combobox = QComboBox()
        self.lon_combobox.addItems(["东经", "西经"])
        self.input_lon = QLineEdit()
        self.input_lon.setPlaceholderText("DDD MM SSS")

        self.label_lat = QLabel("纬度：")
        self.lat_combobox = QComboBox()
        self.lat_combobox.addItems(["北纬", "南纬"])
        self.input_lat = QLineEdit()
        self.input_lat.setPlaceholderText("DD MM SSS")

        self.button_conv = QPushButton("转换")
        self.button_clear = QPushButton("清空")

        layout = QVBoxLayout()

        lon_layout = QHBoxLayout()
        lon_layout.addWidget(self.label_lon)
        lon_layout.addWidget(self.lon_combobox)
        lon_layout.addWidget(self.input_lon)

        lat_layout = QHBoxLayout()
        lat_layout.addWidget(self.label_lat)
        lat_layout.addWidget(self.lat_combobox)
        lat_layout.addWidget(self.input_lat)

        button_layout = QHBoxLayout()
        button_layout.addWidget(self.button_conv)
        button_layout.addWidget(self.button_clear)

        layout.addLayout(lon_layout)
        layout.addLayout(lat_layout)
        layout.addLayout(button_layout)

        self.setLayout(layout)

        self.button_conv.clicked.connect(self.convert)
        self.button_clear.clicked.connect(self.clear)

    def convert(self):
        lon_direction = self.lon_combobox.currentText()
        lat_direction = self.lat_combobox.currentText()
        lon_value = self.input_lon.text().strip(" ")
        lat_value = self.input_lat.text().strip(" ")

        if lon_value and lat_value:
            lon = lon_value + " E" if lon_direction == "东经" else lon_value + " W"
            lat = lat_value + " N" if lat_direction == "北纬" else lat_value + " S"
            if len(lon.split()) != 4 or len(lat.split()) != 4:
                self.msg_box(QMessageBox.Icon.Warning, "警告", "请输入正确的经纬度值")
                return
            lon_dd, lat_dd = self.dms_to_dd(lon, lat)
            self.msg_box(
                QMessageBox.Icon.Information, "计算结果", f"经度：{lon_dd}, 纬度：{lat_dd}"
            )

        else:
            self.msg_box(QMessageBox.Icon.Warning, "警告", "请输入正确的经纬度值")

    def clear(self):
        self.input_lon.clear()
        self.input_lat.clear()

    def msg_box(self, msg_type: QMessageBox.Icon, title: str, message: str):
        """

        :param msg_type:
        :param title:
        :param message:
        :return:
        """
        msg_box = QMessageBox(self)
        msg_box.setIcon(msg_type)
        msg_box.setText(message)
        msg_box.setWindowTitle(title)
        msg_box.exec()

    @staticmethod
    def dms_to_dd(longitude: str, latitude: str) -> tuple:
        def convert_dms_to_dd(dms_str):
            # 实现转换逻辑
            dms = dms_str.split()
            degrees = float(dms[0])
            minutes = float(dms[1])
            seconds = float(dms[2])
            direction = dms[3]

            dd = degrees + minutes / 60 + seconds / 3600

            if direction in ["S", "W"]:
                dd *= -1

            return dd

        longitude_dd = convert_dms_to_dd(longitude)
        latitude_dd = convert_dms_to_dd(latitude)

        return longitude_dd, latitude_dd
